/*
 Copyright 2007 Ole Rogeberg

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */
package org.lifeyears.model;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.lifeyears.model.utils.ToStringUtil;

/**
 * Base class all entity-classes inherits.
 * 
 * @author Aleksander Blomskøld
 */
@MappedSuperclass
public abstract class AbstractEntity
    implements Serializable
{
    @Id
    @GeneratedValue( strategy = GenerationType.AUTO )
    protected Long id;

    // when this object was created, is set by org.lifeyears.hibernate.HibernateEventListener
    @Temporal( TemporalType.TIMESTAMP )
    protected Date creationDate;

    // when this object was last modified, is set by org.lifeyears.hibernate.HibernateEventListener
    @Temporal( TemporalType.TIMESTAMP )
    protected Date modificationDate;

    public Long getId()
    {
        return id;
    }

    public void setId( final Long id )
    {
        this.id = id;
    }

    /**
     * Default equals implementation which compares the id of the objects.
     * 
     */
    @Override
    public boolean equals( final Object that )
    {
        if ( that == null )
        {
            return false;
        }
        else if ( !this.getClass().equals( that.getClass() ) )
        {
            return false;
        }

        final AbstractEntity other = (AbstractEntity) that;

        if ( other.id != null && id != null && other.id.equals( id ) )
        {
            return true;
        }
        else
        {
            return super.equals( other );
        }
    }

    /**
     * Default hashcode implementation which corresponds to equals. Returns the id if it's not null -
     * otherwise it returns super.hashCode()
     */
    @Override
    public int hashCode()
    {
        if ( id != null )
        {
            return id.intValue();
        }
        else
        {
            return super.hashCode();
        }
    }

    @Override
    public String toString()
    {
        return ToStringUtil.toString( this );
    }

    public Date getCreationDate()
    {
        return creationDate;
    }

    public void setCreationDate( final Date creationDate )
    {
        this.creationDate = creationDate;
    }

    public Date getModificationDate()
    {
        return modificationDate;
    }

    public void setModificationDate( final Date modificationDate )
    {
        this.modificationDate = modificationDate;
    }

}
